//
//  JSInputView.m
//  JSBasicFramework
//
//  Created by Apple on 2019/1/17.
//  Copyright © 2019 Apple. All rights reserved.
//

#import "JSInputView.h"

@interface JSInputView ()

@property (nonatomic, assign) CGFloat contentHeight;
@property (nonatomic, assign) CGFloat distanceFromBottom;


@end

@implementation JSInputView

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super initWithCoder:aDecoder]) {
        [self didInitialize];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self didInitialize];
    }
    return self;
}
- (void)didInitialize{
    @weakify(self)
    _maxLines = 3;
    _contenInsets = UIEdgeInsetsMake(10, 0,10, 0);
    _textView = [QMUITextView new];
    self.textView.qmui_keyboardManager.delegateEnabled = YES;
    _textView.font = UIFontMake(kBodyFontSize);
    _textView.placeholder = @"JSKit InputView";
    _textView.delegate = self;
    [self addSubview:_textView];
    self.textView.qmui_keyboardWillShowNotificationBlock = ^(QMUIKeyboardUserInfo *keyboardUserInfo) {
        @strongify(self)
        [self showToolbarViewWithKeyboardUserInfo:keyboardUserInfo];
    };
    self.textView.qmui_keyboardWillHideNotificationBlock = ^(QMUIKeyboardUserInfo *keyboardUserInfo) {
        @strongify(self)
        [self hideToolbarViewWithKeyboardUserInfo:keyboardUserInfo];
        
    };
}
- (void)layoutSubviews{
    [super layoutSubviews];
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _contentHeight = self.textView.font.lineHeight + 15;
    });
    CGFloat textViewX = self.contenInsets.left;
    CGFloat textViewW = self.width;
    if (self.leftView) {
        CGRect rect = [self caculetCustomViewFrameWithView:self.leftView alignType:self.alignStyle];
        self.leftView.frame = CGRectMake(textViewX, rect.origin.y, rect.size.width, rect.size.height);
    }
    if (self.rightView) {
        
        CGRect rect = [self caculetCustomViewFrameWithView:self.rightView alignType:self.alignStyle];
        self.rightView.frame = CGRectMake(self.width - self.contenInsets.right - rect.size.width, rect.origin.y, rect.size.width, rect.size.height);
        textViewW = self.rightView.left;
    }
     textViewX = self.leftView.right;
    textViewW -= textViewX;
    self.height = self.contentHeight + UIEdgeInsetsGetVerticalValue(self.contenInsets);
    self.bottom = self.distanceFromBottom ?:self.superview.bottom+self.height;
    self.textView.frame = CGRectMake(textViewX, self.contenInsets.top, textViewW, self.height - UIEdgeInsetsGetVerticalValue(self.contenInsets));
    
}
-(CGRect) caculetCustomViewFrameWithView:(UIView *) view alignType:(JSInputViewAlignStyle) alignType{
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat w = view.width;
    CGFloat h = view.height;
    [view sizeToFit];
    if (view.height >= self.contentHeight) {
        h = self.contentHeight;
        w = h / view.height * view.width;
    }
    if (alignType ==  JSInputViewAlignStyleTop) {
        y = self.contenInsets.top;
    }else{
        y = self.height - self.contenInsets.bottom - h;
       
    }
    NSLog(@"YYYY: %f", y);
    return CGRectMake(x, y, w, h);
}

-(void)settingLeftView:(UIView *) leftView alignStyle:(JSInputViewAlignStyle) alignStyle{
    if (_leftView) {
        [_leftView removeFromSuperview];
    }
    self.alignStyle = alignStyle;
    _leftView = leftView;
    [self addSubview:_leftView];
    [self layoutIfNeeded];
}
-(void)settingRightView:(UIView *) rightView alignStyle:(JSInputViewAlignStyle) alignStyle{
    if (_rightView) {
        [_rightView removeFromSuperview];
    }
    self.alignStyle = alignStyle;
    _rightView = rightView;
    [self addSubview:_rightView];
    [self layoutIfNeeded];
}
- (void)setContenInsets:(UIEdgeInsets)contenInsets{
    if (UIEdgeInsetsEqualToEdgeInsets(_contenInsets, contenInsets)) {
        return;
    }
    _contenInsets = contenInsets;
    _contentHeight = self.height - UIEdgeInsetsGetVerticalValue(contenInsets);
    [self setNeedsLayout];
}
- (void)textView:(QMUITextView *)textView newHeightAfterTextChanged:(CGFloat)height{
    if (self.contentHeight >0 && self.contentHeight != height && height <= (textView.font.lineHeight * _maxLines + 15)) {
        _contentHeight = height;
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [UIView animateWithDuration:.35 animations:^{
                [self layoutIfNeeded];
                [self setNeedsLayout];
            }];
        });
    }
}

- (void)showToolbarViewWithKeyboardUserInfo:(QMUIKeyboardUserInfo *)keyboardUserInfo {
    if (keyboardUserInfo) {
        // 相对于键盘
        [QMUIKeyboardManager animateWithAnimated:YES keyboardUserInfo:keyboardUserInfo animations:^{
            CGFloat distanceFromBottom = [QMUIKeyboardManager distanceFromMinYToBottomInView:self.superview keyboardRect:keyboardUserInfo.endFrame];
            self.distanceFromBottom = distanceFromBottom;
            self.layer.transform = CATransform3DMakeTranslation(0, - distanceFromBottom - self.inputView.height, 0);
        } completion:^(BOOL finished) {
        }];
        
    }
}

- (void)hideToolbarViewWithKeyboardUserInfo:(QMUIKeyboardUserInfo *)keyboardUserInfo {
    if (keyboardUserInfo) {
        @weakify(self)
        [QMUIKeyboardManager animateWithAnimated:YES keyboardUserInfo:keyboardUserInfo animations:^{
            @strongify(self)
            self.layer.transform = CATransform3DIdentity;
            self.distanceFromBottom = 0;
        } completion:^(BOOL finished) {
            @strongify(self)
            // 清空
            self.textView.text = nil;
        }];
    }
}

@end
